﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Cfg;

namespace Studio7i.Persistencia.NH
{
    public class NHibernateHelper
    {
        private static ISessionFactory _FabricaSesiones;

        private static ISessionFactory FabricaSesiones
        {
            get
            {
                if (_FabricaSesiones == null)
                {
                    var _Configuracion = new Configuration();
                    _Configuracion.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
                    _Configuracion.SetProperty("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
                    _Configuracion.SetProperty("connection.connection_string", ConexionUtil.ObtenerCadena());
                    _Configuracion.SetProperty("adonet.batch_size", "10");
                    _Configuracion.SetProperty("show_sql", "true");
                    _Configuracion.SetProperty("dialect", "NHibernate.Dialect.MsSql2000Dialect");
                    _Configuracion.SetProperty("command_timeout", "60");
                    _Configuracion.SetProperty("query.substitutions", "true 1, false 0, yes 'Y', no 'N'");
                    _Configuracion.AddAssembly(typeof(NHibernateHelper).Assembly);
                    _FabricaSesiones = _Configuracion.BuildSessionFactory();
                }
                return _FabricaSesiones;
            }
        }

        public static ISession ObtenerSesion()
        {
            return FabricaSesiones.OpenSession();
        }
        public static void CerrarFabrica()
        {
            _FabricaSesiones = null;
        }

    }
}